package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.account.AccountSpecialBuyOrderIdDetail;
import com.roc.steelend.controller.vo.account.AccountSpecialBuyReception;
import com.roc.steelend.controller.vo.invoice.InvoiceByBuyOrder;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceByBuyReception;
import com.roc.steelend.controller.vo.specialPay.SpecialByBuyReception;
import com.roc.steelend.mapper.*;
import com.roc.steelend.pojo.Account;
import com.roc.steelend.pojo.CompanyMoney;
import com.roc.steelend.pojo.Invoice;
import com.roc.steelend.pojo.Suppliers;
import com.roc.steelend.service.AccountService;
import com.roc.steelend.service.InvoiceService;
import com.roc.steelend.service.SuppliersService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class SuppliersServiceImpl implements  SuppliersService{


    @Autowired
    private SuppliersMapper suppliersMapper;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private CompanyMoneyMapper companyMoneyMapper;
    @Autowired
    private InvoiceMapper invoiceMapper;


    @Autowired
    private AccountService accountService;
    @Autowired
    private InvoiceService invoiceService;

    @Override
    public List<Suppliers> findSupplierList(String searchMsg, Integer now, Integer size) {
        QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
        if (searchMsg!=null){
            wrapper.like("supplier_name",searchMsg);
        }
        now=(now-1)*size;
        wrapper.last("LIMIT "+now+" , "+size);
        return suppliersMapper.selectList(wrapper);
    }

    @Override
    public Integer countSupplier(String searchMsg) {
        QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
        if (searchMsg!=null){

            wrapper.like("supplier_name",searchMsg);
        }
        return suppliersMapper.selectCount(wrapper);
    }

    @Override
    public Boolean registeredSupplier(Suppliers suppliers) {
        int insert = suppliersMapper.insert(suppliers);
        return insert == 1;
    }

    @Override
    public List<Suppliers> findSuppliersByOrderAdd() {
        QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("supplier_id");
        wrapper.last("LIMIT 1");
        List<Suppliers> suppliers = suppliersMapper.selectList(wrapper);
        return suppliers;

    }

    @Override
    public Suppliers supplierTiaoPiaoAndReconciliation(Integer supplierId) {
        QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();


        wrapper.eq("supplier_id",supplierId);

        Suppliers suppliers = suppliersMapper.selectList(wrapper).get(0);

        return suppliers;
    }

    @Override
    public ReturnMsg postAccountSupplier(SpecialByBuyReception specialByBuyReception, Integer employeeId) {

        List<HashMap<String, Object>> hashMaps = buyOrderMapper.selectAllBuSuppliersId(specialByBuyReception.getSupplierId());
        if (hashMaps.isEmpty()){
            Account account = new Account();
            account.setAccountId(LshUtil.getAccountSpecialLsh());
            account.setEmployeeId(employeeId);
            account.setPayTakeId(specialByBuyReception.getPayOrTakeWay());
            account.setSmallAccountId(2);
            account.setOperateTime(new Timestamp(new java.util.Date().getTime()));
            account.setAccountTime(specialByBuyReception.getPayTime());
            account.setMoney(specialByBuyReception.getPayMoney());
            account.setWho(specialByBuyReception.getSupplierName());
            if (Objects.equals(specialByBuyReception.getRemark(), "")){
                specialByBuyReception.setRemark(null);
            }
            account.setRemark(specialByBuyReception.getRemark());
            QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
            wrapper.eq("supplier_id", specialByBuyReception.getSupplierId());
            Suppliers suppliers = suppliersMapper.selectList(wrapper).get(0);
            if (specialByBuyReception.getIsOver()){
                suppliers.setDebt(new BigDecimal("0"));
            }else {
                suppliers.setDebt(suppliers.getDebt().subtract(specialByBuyReception.getPayMoney()));

            }


            int i = suppliersMapper.updateById(suppliers);

            QueryWrapper<CompanyMoney> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("pay_take_id", specialByBuyReception.getPayOrTakeWay());
            CompanyMoney companyMoney = companyMoneyMapper.selectList(wrapper1).get(0);
            companyMoney.setMoney(companyMoney.getMoney().subtract(specialByBuyReception.getPayMoney()));
            int i1 = companyMoneyMapper.updateById(companyMoney);
//            炒鸡特殊情况，供应商界面付款时没有未付款订单，说明调账后根本就没有再下单了
            account.setWay(1);
            int insert = accountMapper.insert(account);
            ReturnMsg msg = new ReturnMsg();
            if (insert==1&&i==1&&i1==1){
                msg.setCode(1);
                msg.setMsg("采购付款账务操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("采购付款账务操作失败");
            }
            return msg;
        }else {
            ArrayList<AccountSpecialBuyOrderIdDetail> accountSpecialBuyOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
               String buy_order_id = hashMap.get("buy_order_id").toString();
                BigInteger bigInteger = new BigInteger(buy_order_id);
                Date storage_date = (Date) hashMap.get("storage_date");
                AccountSpecialBuyOrderIdDetail accountSpecialBuyOrderIdDetail = new AccountSpecialBuyOrderIdDetail(bigInteger, storage_date);
                accountSpecialBuyOrderIdDetails.add(accountSpecialBuyOrderIdDetail);
            }
            AccountSpecialBuyReception accountSpecialBuyReception = new AccountSpecialBuyReception();
            accountSpecialBuyReception.setSupplierId(specialByBuyReception.getSupplierId());
            accountSpecialBuyReception.setBuyOrderIdList(accountSpecialBuyOrderIdDetails);
            accountSpecialBuyReception.setSupplierName(specialByBuyReception.getSupplierName());
            if (Objects.equals(specialByBuyReception.getRemark(), "")){
                specialByBuyReception.setRemark(null);
            }
            accountSpecialBuyReception.setRemark(specialByBuyReception.getRemark());
            accountSpecialBuyReception.setPayOrTakeWay(specialByBuyReception.getPayOrTakeWay());
            accountSpecialBuyReception.setPayTime(specialByBuyReception.getPayTime());
            accountSpecialBuyReception.setShouldGiveMoney(specialByBuyReception.getDebt());
            accountSpecialBuyReception.setPayMoney(specialByBuyReception.getPayMoney());
            accountSpecialBuyReception.setIsOver(specialByBuyReception.getIsOver());

            return  accountService.postAccountSpecialBuy(accountSpecialBuyReception, employeeId);

        }
    }

    @Override
    public ReturnMsg postInvoiceSupplier(SpecialInvoiceByBuyReception specialInvoiceByBuyReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
        List<HashMap<String, Object>> hashMaps = buyOrderMapper.selectAllBuSuppliersIdInovice(specialInvoiceByBuyReception.getSupplierId());
        if (hashMaps.isEmpty()){
            Invoice invoice = new Invoice();
            invoice.setInvoiceId(LshUtil.getInvoiceBuy());
            invoice.setInvoiceType("采购开票(特殊)");
            invoice.setEmployeeId(employeeId);
            invoice.setOperateTime(new Timestamp(new java.util.Date().getTime()));
            invoice.setInvoiceMoney(specialInvoiceByBuyReception.getInvoiceMoney());
            invoice.setInvoiceTime(specialInvoiceByBuyReception.getInvoiceTime());
            invoice.setWho(specialInvoiceByBuyReception.getSupplierName());
            if (Objects.equals(specialInvoiceByBuyReception.getRemark(), "")){
                specialInvoiceByBuyReception.setRemark(null);
            }
            invoice.setRemark(specialInvoiceByBuyReception.getRemark());
            if (Objects.equals(specialInvoiceByBuyReception.getInvoiceTitle(),"")){
                specialInvoiceByBuyReception.setInvoiceTitle(null);
            }
            invoice.setInvoiceTitle(specialInvoiceByBuyReception.getInvoiceTitle());
            int insert = invoiceMapper.insert(invoice);

            QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
            suppliersQueryWrapper.eq("supplier_id",specialInvoiceByBuyReception.getSupplierId());
            Suppliers suppliers = suppliersMapper.selectList(suppliersQueryWrapper).get(0);
            if (specialInvoiceByBuyReception.getIsOver()){
                suppliers.setInvoice(new BigDecimal("0"));
            }else {
                suppliers.setInvoice(suppliers.getInvoice().subtract(specialInvoiceByBuyReception.getInvoiceMoney()));

            }
            int i = suppliersMapper.updateById(suppliers);
            if (insert==1&&i==1){
                msg.setCode(1);
                msg.setMsg("采购开票操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("采购开票操作失败");
            }
            return msg;

        }else {
            ArrayList<AccountSpecialBuyOrderIdDetail> accountSpecialBuyOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
                String buy_order_id = hashMap.get("buy_order_id").toString();
                BigInteger bigInteger = new BigInteger(buy_order_id);
                Date storage_date = (Date) hashMap.get("storage_date");
                AccountSpecialBuyOrderIdDetail accountSpecialBuyOrderIdDetail = new AccountSpecialBuyOrderIdDetail(bigInteger, storage_date);
                accountSpecialBuyOrderIdDetails.add(accountSpecialBuyOrderIdDetail);
            }

            InvoiceByBuyOrder invoiceByBuyOrder = new InvoiceByBuyOrder();
            invoiceByBuyOrder.setBuyOrderIdList(accountSpecialBuyOrderIdDetails);
            if (Objects.equals(specialInvoiceByBuyReception.getRemark(), "")){
                specialInvoiceByBuyReception.setRemark(null);
            }
            if (Objects.equals(specialInvoiceByBuyReception.getInvoiceTitle(), "")){
                specialInvoiceByBuyReception.setInvoiceTitle(null);
            }
            invoiceByBuyOrder.setRemark(specialInvoiceByBuyReception.getRemark());
            invoiceByBuyOrder.setSupplierId(specialInvoiceByBuyReception.getSupplierId());
            invoiceByBuyOrder.setSupplierName(specialInvoiceByBuyReception.getSupplierName());
            invoiceByBuyOrder.setShouldInvoiceMoney(specialInvoiceByBuyReception.getNoInvoice());
            invoiceByBuyOrder.setInvoiceMoney(specialInvoiceByBuyReception.getInvoiceMoney());
            invoiceByBuyOrder.setIsOver(specialInvoiceByBuyReception.getIsOver());
            invoiceByBuyOrder.setInvoiceTime(specialInvoiceByBuyReception.getInvoiceTime());
            invoiceByBuyOrder.setInvoiceTitle(specialInvoiceByBuyReception.getInvoiceTitle());

            return invoiceService.invoiceBuy(invoiceByBuyOrder,employeeId);
        }

    }
}
